﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WeifenLuo.WinFormsUI.Base;
using WeifenLuo.WinFormsUI.Security;

namespace HRM
{
    public partial class FormProcedureSecurity : FormBaseDataGrid
    {
        private Ctrl_Security mCtrlScuri;
        private DataSet mdsGroups;
        private DataSet mDs_AccessRight;
        private string ms_ID_Group, ms_ID_Module;
        private int mi_CurrentIndexModule = 0;

        public FormProcedureSecurity()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            base.BasicForm_Load(sender, e);
            SetAdd(false);
            SetDelete(false);
            cboModules.Text = "HumanResource";
        }

        protected override void BaseForm_Activated(object sender, System.EventArgs e)
        {
            base.BaseForm_Activated(sender, e);
        }

        protected override void BaseForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            base.BaseForm_KeyDown(sender, e);
        }

        protected override void BasicForm_Closing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
        }

        protected override void LoadMasterData()
        {
            btnCancel = btncancel;
            btnOk = btnok;
            lblStatus = lblstatus;
            LoadData();
            ms_ID_Module = "HumanResource";
            LoadObjectsData(ms_ID_Group, ms_ID_Module, chkReport.Checked);
            base.LoadMasterData();
        }

        private void LoadObjectsData(string as_ID_Group, string as_moduleName, bool ab_ReportType)
        {	// We cannot use the ObjectSet from Contructor 
            // Because in this case Contrustor cannot load and update more than two tables
            //	so we should use DataSet directly
            //if (mdbg_dataGrid.Rows.Count > 0) mdbg_dataGrid.DataSource = null;
            try
            {
                if (mCtrlScuri == null) mCtrlScuri = new Ctrl_Security();
                mDs_AccessRight = mCtrlScuri.GetDSInGroupModule(as_ID_Group, as_moduleName, "VN", ab_ReportType);
                if (mDs_AccessRight == null) return;

                DataColumn[] arr = mDs_AccessRight.Tables["ObjectsGroups"].PrimaryKey;

                mdbg_dataGrid.DataSource = mDs_AccessRight.Tables[0];
                mRowManager = (CurrencyManager)mdbg_dataGrid.BindingContext[mDs_AccessRight, "ObjectsGroups"];
                // How to refresh the status of the row position?
                mRowManager.PositionChanged += new EventHandler(RowMaster_Changed);

                if (chkReport.Checked)
                {
                    mdbg_dataGrid.Columns["Add_Access"].Visible =
                    mdbg_dataGrid.Columns["Modify_Access"].Visible =
                    mdbg_dataGrid.Columns["Delete_Access"].Visible = false;
                }
                else
                {
                    mdbg_dataGrid.Columns["Add_Access"].Visible =
                    mdbg_dataGrid.Columns["Modify_Access"].Visible =
                    mdbg_dataGrid.Columns["Delete_Access"].Visible = true;
                }
            }
            catch (Exception ex)
            {
                Ctrl_LOG.Print("Procedure Security", "LoadObjectsData", "Error message: " + ex.Message);
            }
        }

        private void LoadData()
        {
            try
            {
                if (mCtrlScuri == null) mCtrlScuri = new Ctrl_Security();
                mdsGroups = new DataSet();
                mdsGroups = mCtrlScuri.GetGroups();

                DataView lGroupsView = mdsGroups.Tables[0].DefaultView;
                lGroupsView.Sort = "ID_GROUP";
                lstGroups.DisplayMember = lGroupsView.Sort;
                lstGroups.Items.Clear();

                foreach (DataRowView lOV in lGroupsView)
                    lstGroups.Items.Add(lOV);

                lstGroups.SelectedIndex = 0;
                if (cboModules.Items.Count > 0) cboModules.Items.Clear();
                cboModules.Items.Add("Admin");
                cboModules.Items.Add("DataCenter");
                cboModules.Items.Add("HumanResource");
                cboModules.Items.Add("TimeAttendance");
                cboModules.Items.Add("Payroll");
                cboModules.Items.Add("Insurance");
                cboModules.SelectedIndex = mi_CurrentIndexModule = 0;
                switch (cboModules.SelectedIndex)
                {
                    case 0: ms_ID_Module = "Admin";
                        break;
                    case 1: ms_ID_Module = "DataCenter";
                        break;
                    case 2: ms_ID_Module = "HumanResource";
                        break;
                    case 3: ms_ID_Module = "TimeAttendance";
                        break;
                    case 4: ms_ID_Module = "Payroll";
                        break;
                    case 5: ms_ID_Module = "Insurance";
                        break;
                    default: break;
                }
            }
            catch (Exception ex)
            {
                Ctrl_LOG.Print("Procedure Security", "Load data", "Error message: " + ex.Message);
            }
        }

        public override void modify()
        {
            base.modify();
            mdbg_dataGrid.AllowUserToAddRows = false;
            mdbg_dataGrid.Columns["Description"].ReadOnly = true;
        }

        public override void save()
        {
            mRowManager.EndCurrentEdit();
            if (!mDs_AccessRight.HasChanges())
            {
                base.save();
                SetAdd(false);
                SetDelete(false);
                return;
            }

            if (mCtrlScuri.UpdateAccessRight(mDs_AccessRight))
            {
                base.save();
                SetAdd(false);
                SetDelete(false);
            }
            else
            {
                MessageBox.Show("DB Error", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void cancel()
        {
            mDs_AccessRight.RejectChanges();
            base.cancel();
            SetAdd(false);
            SetDelete(false);
        }

        private void cboModules_SelectedValueChanged(object sender, System.EventArgs e)
        {
            // Making a confirmation to the user to saves or not saves their change of the RIGHT on Objects or Reports.
            if (GetCurrentAction() == MODES.MODIFY)
            {
                switch (MessageBox.Show("Do you really wish to save the changes?", ConstantKeys.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    case DialogResult.Yes:
                        save();
                        break;
                    case DialogResult.No:
                        cancel();
                        break;
                }
            }
            DataRow dr = mdsGroups.Tables[0].Rows[lstGroups.SelectedIndex];
            ms_ID_Group = dr["ID_Group"].ToString();
            switch (cboModules.SelectedIndex)
            {
                case 0: ms_ID_Module = "Admin";
                    break;
                case 1: ms_ID_Module = "DataCenter";
                    break;
                case 2: ms_ID_Module = "HumanResource";
                    break;
                case 3: ms_ID_Module = "TimeAttendance";
                    break;
                case 4: ms_ID_Module = "Payroll";
                    break;
                case 5: ms_ID_Module = "Insurance";
                    break;
                default: break;
            }
            mi_CurrentIndexModule = cboModules.SelectedIndex;
            LoadObjectsData(ms_ID_Group, ms_ID_Module, chkReport.Checked);
            base.LoadMasterData();
        }

        private void lstGroups_SelectedIndexChanged(object sender, System.EventArgs e)
        {	// Making a confirmation to the user to Save or not save their change of the RIGHT on Objects or Reports.
            if (GetCurrentAction() == MODES.MODIFY)
            {
                switch (MessageBox.Show("Do you really wish to save the changes?", ConstantKeys.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    case DialogResult.Yes:
                        save();
                        break;
                    case DialogResult.No:
                        cancel();
                        break;
                }
            }
            DataRow dr = mdsGroups.Tables[0].Rows[lstGroups.SelectedIndex];
            ms_ID_Group = dr["ID_Group"].ToString();
            LoadObjectsData(ms_ID_Group, ms_ID_Module, chkReport.Checked);
            base.LoadMasterData();
        }

        private void chkReport_CheckedChanged(object sender, System.EventArgs e)
        {	// Making a confirmation to the user to Save or not save their change of the RIGHT on Objects or Reports.
            if (GetCurrentAction() == MODES.MODIFY)
            {
                switch (MessageBox.Show("Do you really wish to save the changes", ConstantKeys.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    case DialogResult.Yes:
                        save();
                        break;
                    case DialogResult.No:
                        cancel();
                        break;
                }
            }

            DataRow dr = mdsGroups.Tables[0].Rows[lstGroups.SelectedIndex];
            ms_ID_Group = dr["ID_Group"].ToString();
            switch (cboModules.SelectedIndex)
            {
                case 0: ms_ID_Module = "Admin";
                    break;
                case 1: ms_ID_Module = "DataCenter";
                    break;
                case 2: ms_ID_Module = "HumaneResource";
                    break;
                case 3: ms_ID_Module = "TimeAttendance";
                    break;
                case 4: ms_ID_Module = "Payroll";
                    break;
                case 5: ms_ID_Module = "Insurance";
                    break;
                default: break;
            }
            mi_CurrentIndexModule = cboModules.SelectedIndex;
            LoadObjectsData(ms_ID_Group, ms_ID_Module, chkReport.Checked);

            base.LoadMasterData();
        }

        private void btnok_Click(object sender, System.EventArgs e)
        {
            save();
        }

        private void btncancel_Click(object sender, System.EventArgs e)
        {
            cancel();
        }

        private void mdbg_dataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4 && GetCurrentAction() == MODES.MODIFY)
            {
                if ((bool)mdbg_dataGrid["Read_Access", e.RowIndex].Value == false)
                {
                    mdbg_dataGrid["Add_Access", e.RowIndex].Value =
                    mdbg_dataGrid["Delete_Access", e.RowIndex].Value =
                    mdbg_dataGrid["Modify_Access", e.RowIndex].Value =
                    mdbg_dataGrid["Print_Access", e.RowIndex].Value = false;
                }
            }
        }
    }
}